package com.power200.android.activity;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.power200.android.R;
import com.power200.android.bean.BackLog;
import com.power200.android.service.BackLogService;

import java.util.List;

public class BacklogFragment extends Fragment {


    private List<BackLog> backLogList;
    private BackLogService backLogService = BackLogService.getInstance();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_backlog, container, false);
        processBacklogListView(view, inflater);
        return view;
    }

    private void processBacklogListView(View view, LayoutInflater inflater) {
        ListView backlogListView = (ListView) view.findViewById(R.id.backlog);
        backLogList = backLogService.getBackLogs();
        Log.i("Hell", "Bak");
        backlogListView.setAdapter(new BacklogAdapter(inflater, backLogList));
    }

    public final class ViewHolder {
        protected TextView idText;
        protected TextView priorityText;
        protected TextView nameText;
        protected TextView briefText;
        protected TextView descriptionText;
        protected Button editButton;
    }


    public class BacklogAdapter extends BaseAdapter {

        private LayoutInflater layoutInflater;
        private List<BackLog> backLogs;
        private View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                v.get
            }
        };


        public BacklogAdapter(LayoutInflater inflater, List<BackLog> backLogs) {
            this.layoutInflater = inflater;
            this.backLogs = backLogs;
        }

        @Override
        public int getCount() {
            return backLogs.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // position 是界面的position,不是data的position
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = layoutInflater.inflate(R.layout.backlog_item, null);
                holder.idText = (TextView) convertView.findViewById(R.id.backlog_item_id);
                holder.priorityText = (TextView) convertView.findViewById(R.id.backlog_item_priority);
                holder.nameText = (TextView) convertView.findViewById(R.id.backlog_item_name);
                holder.briefText = (TextView) convertView.findViewById(R.id.backlog_item_brief);
                holder.descriptionText = (TextView) convertView.findViewById(R.id.backlog_item_description);
//                holder.editButton = (Button) convertView.findViewById(R.id.backlog_item_edit);
//                holder.editButton.setOnClickListener(listener);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.idText.setText("" + backLogs.get(position).getId());
            holder.priorityText.setText("" + backLogs.get(position).getPriority());
            holder.nameText.setText(backLogs.get(position).getName());
            holder.briefText.setText(backLogs.get(position).getBrief());
            holder.descriptionText.setText(backLogs.get(position).getDescription());
            return convertView;
        }
    }

}
